package research.dao;

import org.hibernate.Session;

import research.dao.hibernate.HibernateUtil;
import research.entity.EntityType;

public class HibernateDAOFactory extends DAOFactory {

	private GenericHibernateDAO<?, ?> instantiateDAO(Class<?> daoClass) {
		try {
			GenericHibernateDAO<?, ?> dao = (GenericHibernateDAO<?, ?>) daoClass
					.newInstance();
			dao.setSession(getCurrentSession());
			return dao;
		} catch (Exception ex) {
			throw new RuntimeException("Can not instantiate DAO: " + daoClass,
					ex);
		}
	}

	protected Session getCurrentSession() {
		return HibernateUtil.getSessionFactory().getCurrentSession();
	}

	@Override
	public GenericDAO<?, ?> getEntityDAO(EntityType type) {
		return (GenericDAO<?, ?>) instantiateDAO(GenericHibernateDAO.class);
	}
	
}